class Video < ActiveRecord::Base
  acts_as_taggable
  
  validates_presence_of :title
  validates_presence_of :title

  def self.sync_with_vimeo
    url = URI.parse("http://www.vimeo.com/api/#{Settings.vimeo_user}/clips/xml")
    xml = Net::HTTP.get(url)
    doc = REXML::Document.new xml
    Video.transaction do
      doc.elements.each('clips/clip') do |clip|
        @clip = Video.find_or_initialize_by_clip_id(clip.elements['clip_id'].text)
        if @clip.new_record?
          @clip.title = clip.elements['title'].text unless clip.elements['title'].text.nil?
          @clip.body = clip.elements['caption'].text unless clip.elements['caption'].text.nil?
          @clip.photo_url = clip.elements['thumbnail_large'].text
          @clip.tag_list = clip.elements['tags'].text  unless clip.elements['tags'].text.nil?
          @clip.save
        end
      end
    end
  end
  
  def embed_code(width=400, height=300)
    "<object type='application/x-shockwave-flash' width='#{width}' height='#{height}' data='http://vimeo.com/moogaloop.swf?clip_id=#{clip_id}&amp;server=vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=01AAEA'>	<param name='quality' value='best' />	<param name='allowfullscreen' value='true' />	<param name='scale' value='showAll' />	<param name='movie' value='http://vimeo.com/moogaloop.swf?clip_id=#{clip_id}&amp;server=vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=01AAEA' /></object>"
  end
  
  named_url :key => 'title', :url_column => 'url_slug'
  
end
